package com.sync.syncmasterdata.customer.service.convert;

import com.sync.syncmasterdata.customer.pojo.CrmCustomer;
import com.sync.syncmasterdata.customer.pojo.bip.CustomerForBip;
import com.sync.syncmasterdata.customer.pojo.bip.CustomerForBipUpdate;
import com.sync.syncmasterdata.customer.service.impl.CPServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.sync.syncmasterdata.util.NormalUtil.getMapValue;

/**
 * crm 对象 转 Bip
 * crm 对象 prod 操作类别 add  edit
 */
@Service
@Slf4j
public class CrmToBip {
    @Autowired
    private CPServiceImpl cpService;

    /**
     * 最终返回为Bip 客户对象 客户编码使用BIP自动生成 中途转换的时候就要上线
     * @param cus
     * @param prod
     * @return
     */
    public CustomerForBip convertFun(CrmCustomer cus) {
        //获取CRM在BPM中的字典 最终字典要放入BIP中
//        Map<String, JSONObject> priMap = cpService.getAreaMap(new JSONArray()).get("crm");

//        if (priMap.size() < 1) {
//            log.info("crmToU8转换：地址map为空-请检查请求权限");
//            return null;
//        }

        //省
//        String sheng = getMapValue(priMap, cus.getNew_province()) == null ? "" : getMapValue(priMap, cus.getNew_province()).get("new_area").toString();
        //市
//        String shi = getMapValue(priMap, cus.getNew_city()) == null ? "" : getMapValue(priMap, cus.getNew_city()).get("new_area").toString();
        CustomerForBip.CustomerBill.CustomerBillHead customerBillHead = new CustomerForBip.CustomerBill.CustomerBillHead();

        //定义BIP客户基础资料范围
        customerBillHead.setPk_group("BS");
        customerBillHead.setPk_org("BS");
        //客户编码
        customerBillHead.setCode(cus.getNew_num());
        //只使用crm中的客户名称 作为BIP->name
        customerBillHead.setName(cus.getName());
        //客户简称-同上
        customerBillHead.setShortname(cus.getName());
        //客户类型
        customerBillHead.setCustprop(0);
        //客户基本分类编码
        customerBillHead.setPk_custclass(custclassConvert(cus.getNew_type()));
        //纳税人识别号 -- todo:cmr增加字段 必填
//        customerBillHead.setTaxpayerid("");
        //国家地区 -- todo:先做固定
        customerBillHead.setPk_country("CN");
        //备注
        customerBillHead.setMemo("通过crm推送客户信息");
        //自定义项
        customerBillHead.setDef1(cus.getAccountid());
        customerBillHead.setDef2(cus.getNew_erp_num());
        customerBillHead.setDef3(cus.getAccountid());
        //地区转换 --todo:地区转换编码
//        customerBillHead.setPk_areacl("");
        //以下增加其他必要字段转换

        //主表内容转换 基本保持默认值
        CustomerForBip.CustomerBill customerBill= CustomerForBip.CustomerBill.builder().billhead(customerBillHead).build();
        CustomerForBip customerBip=CustomerForBip.builder().bill(customerBill).build();

        return customerBip;

    }

    public CustomerForBipUpdate convertFunUpdate(CrmCustomer cus,String id) {
        CustomerForBipUpdate.CustomerBill.CustomerBillHead customerBillHead = new CustomerForBipUpdate.CustomerBill.CustomerBillHead();

        //定义BIP客户基础资料范围
        customerBillHead.setPk_group("BS");
        customerBillHead.setPk_org("BS");
        //客户编码
        customerBillHead.setCode(cus.getNew_num());
        //只使用crm中的客户名称 作为BIP->name
        customerBillHead.setName(cus.getName());
        //客户简称-同上
        customerBillHead.setShortname(cus.getName());
        //客户类型
        customerBillHead.setCustprop(0);
        //客户基本分类编码
        customerBillHead.setPk_custclass(custclassConvert(cus.getNew_type()));
        //国家地区 -- todo:先做固定
        customerBillHead.setPk_country("CN");
        //备注
        customerBillHead.setMemo("crm更新信息");
        //自定义项
        customerBillHead.setDef1(cus.getAccountid());
        customerBillHead.setDef2(cus.getNew_erp_num());
        customerBillHead.setDef3(cus.getAccountid());
        //以下增加其他必要字段转换

        //主表内容转换 基本保持默认值
        CustomerForBipUpdate.CustomerBill customerBill= CustomerForBipUpdate.CustomerBill.builder().billhead(customerBillHead).build();
        customerBill.setId(id);
        CustomerForBipUpdate customerBip=CustomerForBipUpdate.builder().bill(customerBill).build();

        return customerBip;

    }

    /**
     * 以下为本次转换特殊方法
     */
    private String custclassConvert(Object object) {
        try {
            String _obj = object.toString();
            switch (_obj) {
                case "1":
                    return "01";
                case "2":
                    return "03";
                case "4":
                    return "02";
                case "5":
                    return "04";
                default:
                    return "01";
            }
        } catch (Exception e) {
            return "01";
        }
    }
}
